#include "timer.h"

#define DIVISOR 0x04A9 //1193181 / 1000

/** Program timer (0,1,2) with mode
 */
void timer_init(int timer, int mode)
{
	outportb(TIMER_CTRL, (timer << 6) | mode);
}

/** Load timer (0,1,2) with value
 */
void timer_load(int timer, int value)
{
	int note_freq = TIMER_CLK/value;
	timer_init(timer, LCOM_MODE);
	outportb(timer, LSB(note_freq));
	outportb(timer, MSB(note_freq));
}
void mili_sleep(int mili)
{
	int timeout = mili_tick + mili;
	while(mili_tick < timeout);
}

int get_mili_tick() {
	return mili_tick;
}

/*void sec_sleep(int secs)
{
	int timeout = sec_tick + secs;
	while(sec_tick < timeout);
}*/

void init_timer0()
{
	outportb(TIMER_CTRL, LCOM_MODE);

	outportb(TIMER_0, LSB(DIVISOR));	
	outportb(TIMER_0, MSB(DIVISOR));

	outportb(SPEAKER_CTRL,inportb(SPEAKER_CTRL) | (TIMER2_ENABLE));

	enable_irq(T0_IRQ);
}


